﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Business;
using System.Web.Security;

public partial class admin_Create_User : System.Web.UI.Page
{
    //Create new object of decryption and encryption
    private Decryption objdec = new Decryption();
    private Encryption objenc = new Encryption();
    private string client_id;
    private string clientName;

    protected void Page_Load(object sender, EventArgs e)
    {
    }

    /// <summary>
    /// Handles the DataBound event of the FormViewUsers control.
    /// Set the client id, is admin, client name, encrypt the password
    /// </summary>
    /// <param name="sender">The source of the event.</param>
    /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
    protected void FormViewUsers_DataBound(object sender, EventArgs e)
    {
        client_id = objdec.DecodeUrl(Request.QueryString["client_id"].ToString());
        clientName = objdec.DecodeUrl(Request.QueryString["clientName"].ToString());
        clientNameLabel.Text = "for client: " + clientName;
    }

    protected void FormViewUsers_ItemInserted(object sender, FormViewInsertedEventArgs e)
    {
        string client_id = objdec.DecodeUrl(Request.QueryString["client_id"].ToString());
        Server.Transfer("View_Client.aspx?client_id=" + client_id);
    }

    protected void InsertCancelButton_Click(object sender, EventArgs e)
    {
        string client_id = objdec.DecodeUrl(Request.QueryString["client_id"].ToString());
        Server.Transfer("View_Client.aspx?client_id=" + client_id);
    }

    protected void LoginStatus1_LoggingOut(object sender, LoginCancelEventArgs e)
    {
        Session.Contents.Remove("UserDetails");
        Session.Abandon();
        Session.Contents.RemoveAll();
        FormsAuthentication.SignOut();
        Response.Redirect("../Default.aspx");
    }

    protected void FormViewUsers_ItemInserting(object sender, FormViewInsertEventArgs e)
    {
        var client_idTextBox = FormViewUsers.FindControl("client_idTextBox") as TextBox;
//        var is_adminTextBox = FormViewUsers.FindControl("is_adminTextBox") as TextBox;
        var passwordTextBox = FormViewUsers.FindControl("passwordTextBox") as TextBox;
        var confirmPassword = FormViewUsers.FindControl("confirmPassword") as TextBox;
        client_id = objdec.DecodeUrl(Request.QueryString["client_id"].ToString());
        clientName = objdec.DecodeUrl(Request.QueryString["clientName"].ToString());
        client_idTextBox.Text = client_id;
        var pwd = passwordTextBox.Text;
        if(pwd.Equals(string.Empty))
        {
            Response.Write("<SCRIPT LANGUAGE = JavaScript>alert('Password can not be empty.')</SCRIPT>");
            e.Cancel = true;
            return;
        }
        passwordTextBox.Text = objenc.EncryptString(pwd);

        var confirmPwd = confirmPassword.Text;
        if (pwd.Equals(string.Empty))
        {
            Response.Write("<SCRIPT LANGUAGE = JavaScript>alert('Confirm password can not be empty.')</SCRIPT>");
            e.Cancel = true;
            return;
        }
        confirmPassword.Text = objenc.EncryptString(confirmPwd);

        if (!pwd.Equals(confirmPwd))
        {
            Response.Write("<SCRIPT LANGUAGE = JavaScript>alert('Confirm password does not match with password.')</SCRIPT>");
            e.Cancel = true;
            return;
        }

        var dic = e.Values;
        if (pwd == confirmPwd)
        {
            dic["client_id"] = client_id;
            dic["password"] = confirmPassword.Text;
        }
    }
}